import type { VxeTableGridOptions } from '@vben/plugins/vxe-table';

import type { VbenFormSchema } from '#/adapter/form';
import type { OnActionClickFn } from '#/adapter/vxe-table';
import type { ProductStateModel } from '#/api/production/models';
import type { DefectDefineModel } from '#/api/quality/models';

import { getOperationListApi } from '#/api/process/operation';
import { getDefectDefineListApi } from '#/api/quality/defectDefine';

/** 获取表格查询表单的字段配置。 */
export function useSearchSchema(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      componentProps: {
        allowClear: true,
      },
      fieldName: 'sn',
      label: '零件码',
    },
  ];
}

/**
 * 获取编辑表单的字段配置。
 */
export function useSchema(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      fieldName: 'sn',
      disabled: true,
      label: '零件码',
    },
    {
      component: 'ApiSelect',
      componentProps: {
        allowClear: true,
        showSearch: true,
        mode: 'multiple',
        optionFilterProp: 'label',
        afterFetch: (data: DefectDefineModel[]) => {
          // TODO: 采用树结构，参考 "点检标件-工位多选"
          return data.map((item) => ({
            label: `${item.defectCode} - ${item.defectName}`,
            value: item.id,
          }));
        },
        api: getDefectDefineListApi,
        immediate: false,
      },
      fieldName: 'defectIds',
      label: '缺陷问题',
      rules: 'required',
    },
    {
      component: 'Input',
      fieldName: 'operationCode',
      disabled: true,
      label: '不合格工序',
    },
    {
      component: 'ApiSelect',
      componentProps: {
        showSearch: true,
        optionFilterProp: 'label',
        labelField: 'opName',
        valueField: 'id',
        api: getOperationListApi,
        immediate: false,
      },
      fieldName: 'targetOperationId',
      label: '重新上线工序',
      rules: 'required',
    },
    {
      component: 'Textarea',
      componentProps: {
        showCount: true,
        maxlength: 128,
      },
      fieldName: 'remark',
      label: '返工原因',
    },
  ];
}

/**
 * 获取表格列配置。
 * @param onActionClick 表格操作按钮点击事件
 */
export function useColumns(
  onActionClick?: OnActionClickFn<ProductStateModel>,
): VxeTableGridOptions<ProductStateModel>['columns'] {
  return [
    { type: 'seq', width: 50 },
    { field: 'sn', title: '零件码', width: 220 },
    { field: 'operationCode', title: '当前工序', width: 100 },
    { field: 'workstationCode', title: '当前工位', width: 100 },
    { field: 'wo', title: '工单编号', width: 160 },
    {
      field: 'status',
      title: '状态',
      width: 100,
      cellRender: {
        name: 'CellTag',
        options: [
          { color: 'cyan', label: 'OK', value: 1 },
          { color: 'warning', label: 'NG', value: 2 },
          { color: 'cyan', label: '强制OK', value: 3 },
          { color: 'warning', label: '强制NG', value: 4 },
          { color: 'red', label: '报废', value: 5 },
        ],
      },
    },
    { field: 'outboundTime', title: '出站时间', minWidth: 150 },
    {
      cellRender: {
        attrs: {
          onClick: onActionClick,
        },
        name: 'CellOperation',
        options: [{ code: 'rework', text: '返工' }],
      },
      field: 'operation',
      fixed: 'right',
      title: '操作',
      width: 60,
    },
  ];
}
